<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" class="search-container" size="small" :inline="true" label-width="80px">
        <el-form-item label="标题" prop="blogTitle">
          <el-input
            v-model="queryParams.blogTitle"
            placeholder="请输入标题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="文章状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择文章状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.bl_blog_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="评论状态" prop="commentOpen">
          <el-select v-model="queryParams.commentOpen" placeholder="请选择评论状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>-->
        <el-form-item label="是否原创" prop="original">
          <el-select v-model="queryParams.original" placeholder="请选择是否原创" clearable size="small">
            <el-option
              v-for="dict in dict.type.bl_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="推荐等级" prop="blogRecommend">
          <el-select v-model="queryParams.blogRecommend" placeholder="请选择推荐等级" clearable size="small">
            <el-option
              v-for="dict in dict.type.bl_recommend_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="点赞数" prop="blogGoods">
          <el-input
            v-model="queryParams.blogGoods"
            placeholder="请输入点赞数"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="阅读数" prop="blogRead">
          <el-input
            v-model="queryParams.blogRead"
            placeholder="请输入阅读数"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收藏数" prop="blogCollection">
          <el-input
            v-model="queryParams.blogCollection"
            placeholder="请输入收藏数"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评论数" prop="blogComment">
          <el-input
            v-model="queryParams.blogComment"
            placeholder="请输入评论数"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="复核状态" prop="approvalStatus">
          <el-select v-model="queryParams.approvalStatus" placeholder="请选择复核状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.sys_approval_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="blogType" label="文章分类">
          <treeselect
            v-model="queryParams.blogType"
            :options="typeOptions"
            :normalizer="normalizer"
            :show-count="true"
            size="small"
            placeholder="选择文章分类"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['blog:blog:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['blog:blog:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['blog:blog:remove']"
          >删除</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-s-promotion"
            size="mini"
            @click="handleApprove"
            :disabled="multiple"
            v-hasPermi="['blog:blog:approve']"
          >复核</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['blog:blog:export']"

          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        resizable
        :row-config="{isHover: true,height: 90}"
        :data="blogList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full' "
      >
        <vxe-column type="checkbox" width="60" fixed="left" align="center"></vxe-column>
        <vxe-column type="seq" title="序号" width="60" fixed="left" align="center"></vxe-column>
        <vxe-column field="blogImage" title="封面" width="150" align="center">
          <template slot-scope="scope">
            <image-preview v-if="scope.row.blogImage !== null ? scope.row.blogImage : ''" :src="scope.row.blogImage" :width="100" :height="70"/>
          </template>
        </vxe-column>
        <vxe-column field="blogTitle" title="标题" width="280" sortable align="center"></vxe-column>
        <vxe-column field="typeName" title="博客分类" width="150" sortable align="center"></vxe-column>
        <vxe-column field="blogRecommend" title="推荐等级" width="120" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bl_recommend_level" :value="scope.row.blogRecommend"/>
          </template>
        </vxe-column>
        <vxe-column field="original" title="是否原创" width="120" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bl_yes_no" :value="scope.row.original" />
          </template>
        </vxe-column>
        <vxe-column field="status" title="文章状态" width="120" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bl_blog_status" :value="scope.row.status"/>
          </template>
        </vxe-column>
        <vxe-column field="commentOpen" title="评论状态" width="120" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.commentOpen"/>
          </template>
        </vxe-column>
        <vxe-column field="approvalStatus" title="复核状态" width="120" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_approval_status" :value="scope.row.approvalStatus"/>
          </template>
        </vxe-column>
        <vxe-column field="blogGoods" title="点赞数" width="120" sortable align="center"></vxe-column>
        <vxe-column field="blogRead" title="阅读数" width="120" sortable align="center"></vxe-column>
        <vxe-column field="blogCollection" title="收藏数" width="120" sortable align="center"></vxe-column>
        <vxe-column field="blogComment" title="评论数" width="120" sortable align="center"></vxe-column>
        <vxe-column field="createTime" title="创建时间" align="center" sortable width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </vxe-column>
        <vxe-column field="blogRemark" title="简介" width="300" show-overflow header-align="center"/>
        <vxe-column title="操作" fixed="right" width="210" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['blog:blog:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="toRead(scope.row)"
              v-hasPermi="['blog:about:query']"
            >阅读</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['blog:blog:remove']"
            >删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <BlogInfoDialog
      v-if="dialogVisible"
      :title="title"
      :blogInfo="blogInfo"
      @closeDialog="cancel"
      @getList="getList"
    />

    <!-- 阅读弹窗 -->
    <el-dialog
      :title="'阅读文章：' + blogInfo.blogTitle"
      :visible.sync="readDialogVisible"
      v-if="readDialogVisible"
      :destroy-on-close="true"
      v-drag-zoom>
      <div class="read-container">
        <div class="read-title">
          <h1>{{ blogInfo.blogTitle }}</h1>
        </div>
        <div class="read-remark">
          <div><strong>文章简介：</strong>{{ blogInfo.blogRemark }}</div>
        </div>
        <div class="read-content" v-html="blogInfo.blogContent" />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 文章复核 -->
    <el-dialog
      title="文章复核"
      :visible.sync="approveDialogVisible"
      :destroy-on-close="true"
      :modal-append-to-body="true"
      append-to-body
      v-drag-zoom
    >
      <vxe-table
        resizable
        :row-config="{isHover: true,height: 90}"
        :data="selectBlogList"
        class="approve-table-container"
        ref="approveTable"
      >
        <vxe-column type="seq" title="序号" width="60" fixed="left" align="center"></vxe-column>
        <vxe-column field="blogImage" title="封面" width="150" align="center">
          <template slot-scope="scope">
            <image-preview v-if="scope.row.blogImage !== null ? scope.row.blogImage : ''" :src="scope.row.blogImage" :width="100" :height="70"/>
          </template>
        </vxe-column>
        <vxe-column field="blogTitle" title="标题" width="200" sortable align="center"></vxe-column>
        <vxe-column field="typeName" title="博客分类" width="150" sortable align="center"></vxe-column>
        <vxe-column field="blogRecommend" title="推荐等级" width="110" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bl_recommend_level" :value="scope.row.blogRecommend"/>
          </template>
        </vxe-column>
        <vxe-column field="original" title="是否原创" width="110" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bl_yes_no" :value="scope.row.original" />
          </template>
        </vxe-column>
        <vxe-column field="commentOpen" title="评论状态" width="110" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.commentOpen"/>
          </template>
        </vxe-column>
        <vxe-column field="approvalStatus" title="复核状态" width="110" sortable align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_approval_status" :value="scope.row.approvalStatus"/>
          </template>
        </vxe-column>
        <vxe-column field="createTime" title="创建时间" align="center" sortable width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </vxe-column>
        <vxe-column title="操作" fixed="right" width="150" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="toRead(scope.row)"
              v-hasPermi="['blog:about:query']"
            >阅读</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleApproveDelete(scope.row)"
              :disabled="selectBlogList.length === 1"
            >移除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <el-form ref="approveForm" :model="approveForm" :rules="approveFormRules" label-width="80px" class="approve-form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="复核状态" prop="approvalStatus">
              <el-radio-group v-model="approveForm.approvalStatus">
                <el-radio
                  v-for="dict in dict.type.sys_approval_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="复核意见">
              <el-input v-model="approveForm.replyContent" type="textarea" rows="3" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="approveSubmit">提交</el-button>
        <el-button size="mini" @click="closeApproveDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBlog, getBlog, delBlog, approveBlog} from "@/api/blog/blog";
import { listAllType } from '@/api/blog/type'
import { getToken } from '@/utils/auth'
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Prismjs from 'prismjs';
import BlogInfoDialog from '@/views/blog/blog/dialog/index.vue';

export default {
  name: "Blog",
  components: {
    Treeselect,
    BlogInfoDialog
  },
  dicts: ['sys_normal_disable', 'bl_blog_status', 'sys_approval_status',
    'bl_blog_original', 'bl_recommend_level', 'bl_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博客管理表格数据
      blogList: [],
      // 选中的文章列表
      selectBlogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 是否显示阅读弹出层
      readDialogVisible: false,
      // 禁用复核选择
      disableApprovalStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        blogTitle: null,
        blogGoods: null,
        blogRead: null,
        blogCollection: null,
        blogType: null,
        blogRemark: null,
        blogComment: null,
        blogRecommend: null,
        blogSource: null,
        commentOpen: null,
        original: null,
        status: null,
        approvalStatus: null
      },
      headers: {
        // 上传文件的请求头
        Authorization: getToken()
      },
      // 控制添加标签弹窗显示
      addTagDialog: false,
      // 控制文章复核弹框显示
      approveDialogVisible: false,
      // 选中的标签
      tagValues: [],
      // 分类树选项
      typeOptions: [],
      // 文章复核
      approveForm: {
        approvalStatus: null,
        replyContent: null,
        blogIdList: null,
      },
      // 文章复核表单校验
      approveFormRules: {
        approvalStatus: [{required:true, message: '请选择复核状态', trigger: 'blur'}]
      },
      // 表单参数
      blogInfo: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.typeCode,
        label: node.typeName,
        children: node.children
      };
    },
    /** 查询博客管理列表 */
    getList() {
      this.loading = true;
      this.typeOptions = [];
      listBlog(this.queryParams).then(response => {
        this.blogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listAllType().then(res => {
        const type = this.handleTree(res.data, "typeCode", "typeCodeP")
        type.forEach( e => {
          this.typeOptions.push(e)
        })
      })
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.readDialogVisible = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length!==1
      this.multiple = !row.records.length
      this.selectBlogList = row.records;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
      this.title = "添加博客";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBlog(id).then(response => {
        this.blogInfo = response.data;
        this.title = "修改博客";
        this.dialogVisible = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除博客管理编号为"' + ids + '"的数据项？').then(function() {
        return delBlog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 删除复核表的某行数据 */
    handleApproveDelete(row) {
      const $table = this.$refs.approveTable;
      const that = this;
      this.$modal.confirm('是否取消复核文章【"' + row.blogTitle + '"】？').then(function() {
        const index = $table.getRowIndex(row);
        that.selectBlogList.splice(index, 1);
      }).then(() => {
        this.$modal.msgSuccess("移除成功");
      })
    },
    /** 文章审核操作 */
    handleApprove() {
      for (let blog of this.selectBlogList) {
        if (blog.approvalStatus !== '2') {
          this.$message.warning('请选择复核状态为待复核的数据!')
          return;
        }
      }
      this.approveForm = {
        approvalStatus: null,
        replyContent: null,
        blogIdList: null,
      };
      this.approveDialogVisible = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/blog/export', {
        ...this.queryParams
      }, `blog_${new Date().getTime()}.xlsx`)
    },
    /** 文章复核提交 */
    approveSubmit() {
      this.$refs["approveForm"].validate(valid => {
        if (valid) {
          this.approveForm.blogIdList = this.selectBlogList.map(item => item.id);
          console.log(this.approveForm, this.selectBlogList)
          approveBlog(this.approveForm).then(res => {
            this.$message.success('复核成功');
            this.approveDialogVisible = false;
            this.getList();
          })
        }
      });
    },
    /** 关闭复核弹框 */
    closeApproveDialog() {
      this.approveDialogVisible = false;
    },
    /** 阅读 */
    toRead(row) {
      this.blogInfo = row;
      this.readDialogVisible = true;
      this.$nextTick(() => {
        Prismjs.highlightAll();
      })
    },
    /** 表单重置 */
    reset() {
      this.blogInfo = {
        id: null,
        blogTitle: null,
        blogImage: null,
        blogContent: null,
        blogGoods: null,
        blogRead: null,
        blogCollection: null,
        blogType: null,
        blogRemark: null,
        blogComment: null,
        blogRecommend: null,
        blogSource: null,
        commentOpen: null,
        original: null,
        status: null,
        createTime: null,
        updateTime: null,
        version: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        remark: null,
        tagList: []
      };
      this.resetForm("form");
      this.tagValues = []
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .table-container {
  .vxe-table {
    height: calc(100vh - 364.5px);
  }
  .vxe-table--body-wrapper {
    height: calc(100vh - 408.5px);
  }
  .fixed-left--wrapper,
  .fixed-right--wrapper {
    height: calc(100vh - 417.5px);
  }
}
::v-deep .approve-table-container {
  .vxe-table {
    height: calc(75vh - 364.5px);
  }
  .vxe-table--body-wrapper {
    height: calc(75vh - 408.5px);
  }
  .fixed-left--wrapper,
  .fixed-right--wrapper {
    height: calc(75vh - 417.5px);
  }
}

.read-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px 50px;
  .read-title {
    font-size: 20px;
    font-weight: bold;
  }
  .read-remark {
    font-size: 16px;
    padding: 15px;
    background-color: rgba(221, 224, 228, 0.53);
    width: 100%;
  }
}

.approve-form {
  margin-top: 20px;
}
</style>
